/** m2settings is a GUI to configure Maven 2 settings.xml 
 * 
 *  Copyright (C) 2007 Regis Decamps <decamps@users.sf.net>
 * 
 *  This file is part of m2settings.
 *
 *  m2settings is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version. 
 *
 *  Foobar is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *  or read it online http://www.gnu.org/licenses/gpl.txt
 *
 **/
/*
 * RepositoriesTableModel.java
 *
 * Created on May 1, 2007, 9:19 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package info.decamps.m2settings.model;

import javax.swing.event.TableModelEvent;

/**
 *
 * @author regis
 */
public class RepositoriesTableModel extends  AbstractRepositoryTableModel {
    
    private Repositories repositories;
    
    /** Creates a new instance of RepositoriesTableModel */
    public RepositoriesTableModel(Repositories repositories) {
        initColumnTitles();
        if (repositories==null) {
            repositories=new Repositories();
        }
        this.repositories=repositories;
    }
    
    public void removeRows(int[] rowIndexes) {
        for (int i = rowIndexes.length - 1; i >= 0; i--) {
            Repository vRepo=getRow(rowIndexes[i]);
            repositories.removeRepository(vRepo);
            fireTableChanged(new TableModelEvent(this, i, i,
                    TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE));
        }
    }
    
    public String getElementName() {
        return "repository";
    }
    
    public int getRowCount() {
        return repositories.getRepositoryCount();
    }
    
    public Object getValueAt(int rowIndex, int columnIndex) {
        Repository row=getRow(rowIndex);
        switch(columnIndex) {
            case COLUMN_ID: return row.getId();
            case COLUMN_LAYOUT: return row.getLayout();
            case COLUMN_NAME: return row.getName();
            case COLUMN_URL: return row.getUrl();
        }
        return null;
    }
    
    public Repository getRow(int rowIndex) {
        return repositories.getRepository(rowIndex);
    }
    
    public void addRow(Repository vRepository) {
        repositories.addRepository(vRepository);
        fireTableChanged(new TableModelEvent(this, getRowCount(),
                getRowCount(), TableModelEvent.ALL_COLUMNS,
                TableModelEvent.INSERT));
    }
    
    public Repositories getRepositories() {
        return repositories;
    }
}
